package com.huika.renrench.control.my.certification.activity;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.text.Html;
import android.text.InputFilter;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.android.volley.Response.Listener;
import com.google.gson.reflect.TypeToken;
import com.huika.lib.ui.widget.ToastMsg;
import com.huika.renrench.GlobalApp;
import com.huika.renrench.R;
import com.huika.renrench.config.UrlConstants;
import com.huika.renrench.control.common.base.BaseAct;
import com.huika.renrench.http.FormResultRequest;
import com.huika.renrench.http.JSONWrapAjaxParams;
import com.huika.renrench.http.RequestResult;
import com.huika.renrench.view.TitleBarHelper;

/**
 * 
 * @description：认证界面
 * @author zc
 * @date 2015年6月2日 上午11:04:58
 */
public class CertificationActivity extends BaseAct implements Listener<RequestResult<Object>> {

	private EditText name_detail;
	private EditText card_number_detail;
	private Button confirm;
	private TextView propmt;
	private String userId;
	
	@Override
	protected void initData() {
		super.initData();
		TitleBarHelper barHelper = new TitleBarHelper(this, -1);
		barHelper.setLeftMsg("返回");
		barHelper.setTitleMsg("实名认证");
		userId = GlobalApp.userModel.getUserId();
	}
	
	@Override
	public void setRootView() {
		setContentView(R.layout.activity_certification);
	}
	
	@Override
	protected void initWidget() {
		super.initWidget();
		name_detail = (EditText) findViewById(R.id.name_detail);
		card_number_detail = (EditText) findViewById(R.id.card_number_detail);
		confirm = (Button) findViewById(R.id.confirm);
		propmt = (TextView) findViewById(R.id.propmt);
		propmt.setText(Html.fromHtml("1.您提交信息资料后，客服会尽快审核，请保证信息资料的真实性，有助于加快审核速度。<br />2.实名认证成功后，将不能再修改身份信息。<br />3.审核过程如有疑问，请致电客服：<font color=#38bdee>400-613-6358</font>"));
		
		name_detail.setFilters(new InputFilter[]{new WordsLengthFilter(name_detail, 20)});
		confirm.setOnClickListener(this);
	}
	
	/**
	 * 
	 * @description：实名认证
	 * @author zc
	 * @date 2015年6月2日 下午5:48:40
	 */
	private void realNameAuthentication(String userId, String realName, String idCard, String ip, String mac, int dev) {
		showLoadingDialog(getString(R.string.common_loading));
		JSONWrapAjaxParams ajaxParams = new JSONWrapAjaxParams();
		ajaxParams.putCommonTypeParam("userId", userId);
		ajaxParams.putCommonTypeParam("realName", realName);
		ajaxParams.putCommonTypeParam("idCard", idCard);
		ajaxParams.putCommonTypeParam("ip", ip);
		ajaxParams.putCommonTypeParam("mac", mac);
		ajaxParams.putCommonTypeParam("dev", dev);
		FormResultRequest<Object> request = new FormResultRequest<Object>(UrlConstants.REALNAMEAUTHENTICATION, this, this, new TypeToken<RequestResult<Object>>() {
		}.getType());
		request.setRequestParams(ajaxParams);
		executeRequest(request);
	}
	
	private void chekcInput() {
		if(name_detail.getText().length() == 0) {
			ToastMsg.showToastMsg(this, "请输入真实姓名");
			return;
		} else if(card_number_detail.length() < 6) {
			ToastMsg.showToastMsg(this, "请输入正确的身份证号");
			return;
		} 
//		else if(!confirmIDCard_et.getText().toString().equals(_IDCard_et.getText().toString())) {
//			ToastMsg.showToastMsg(this, "两次输入的身份证号不同，请检查后重新输入");
//			return;
//		}
		
		realNameAuthentication(userId, name_detail.getText().toString(), card_number_detail.getText().toString(), GlobalApp.ip, GlobalApp.mac, 2);
	}
	
	/**
	 * 
	 * @description：检查身份证的正则表达式
	 * @author zc
	 * @date 2015年1月23日 下午1:46:41
	 */
	private boolean checkIDCard(String input) {
		Pattern pattern = Pattern.compile("(\\d{14}[0-9a-zA-Z])|(\\d{17}[0-9a-zA-Z])");
		Matcher matcher = pattern.matcher(input);
		return matcher.matches();
	}
	
	@Override
	public void widgetClick(View v) {
		super.widgetClick(v);
		switch (v.getId()) {
			case R.id.confirm:
				chekcInput();
				break;

		}
	}

	@Override
	public void onResponse(RequestResult<Object> arg0) {
		dismissLoadingDialog();
		if(arg0.flag == 1) {
			GlobalApp.userModel.setRealName(name_detail.getText().toString());
			GlobalApp.userModel.setIdCard(card_number_detail.getText().toString());
			finish();
		}
		ToastMsg.showToastMsg(this, arg0.msg);
	}

}
